﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Sklad1_2.Default;

namespace Sklad1_2.Add.Update
{
    public partial class UpdateZakupka : Defaultform
    {
        private int count3;
        private double price1;
        private bool flag;
        private bool flag1;
        private string date;
        public UpdateZakupka()
        {
            InitializeComponent();
        }
        public UpdateZakupka(List<string> item ):base(item)
        {
            InitializeComponent();
            ABase.AddItem(comboBox1, "angar");
            comboBox1.SelectedValue = item[1];
            ABase.AddItem(comboBox2, "category");
            comboBox2.SelectedValue = item[2]; 
            ABase.AddItemFirma(comboBox3, "firma");
            comboBox3.SelectedValue = item[3];
            ABase.AddItemProduct(comboBox4, "product");
            comboBox4.SelectedValue = item[4];
            textBox1.Text = item[5];
            textBox2.Text = item[6];
            textBox3.Text = item[7];
            textBox4.Text = "0";
            textBox7.Text = "0";
            date = item[8];
            textBox5.Text = item[9];
            if (textBox3.Text != textBox5.Text || (textBox3.Text == "0" && textBox5.Text == "0"))
            {
                checkBox2.Enabled = checkBox1.Enabled = button4.Enabled = comboBox1.Enabled = comboBox2.Enabled = comboBox3.Enabled = comboBox4.Enabled = false;
                textBox7.ReadOnly=textBox1.ReadOnly = true;
            }
        }
        public override sealed void Init()
        {
            
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Type t = typeof(Int32);
            if (comboBox1.SelectedValue != null)
            {
                if (comboBox1.SelectedValue.GetType() == t)
                {
                    ABase.AddItemCategory(comboBox2, Convert.ToInt32(comboBox1.SelectedValue));
                    if (comboBox2.SelectedValue == null)
                    {
                        comboBox3.DataSource = null;
                    }
                }
            }
            else
            {
                comboBox2.DataSource = null;
            }
        }

        private void comboBox2_SelectedValueChanged(object sender, EventArgs e)
        {
            Type t = typeof(Int32);
            if (comboBox2.SelectedValue != null)
            {
                if (comboBox2.SelectedValue.GetType() == t)
                {
                    ABase.AddItemFirma(comboBox3, Convert.ToInt32(comboBox2.SelectedValue));
                    if (comboBox3.SelectedValue == null)
                    {
                        comboBox4.DataSource = null;
                    }
                }
            }
            else
            {
                comboBox3.DataSource = null;
            }
        }

        private void comboBox3_SelectedValueChanged(object sender, EventArgs e)
        {
            Type t = typeof(Int32);
            if (comboBox3.SelectedValue != null)
            {
                if (comboBox3.SelectedValue.GetType() == t)
                {
                    ABase.AddItemProduct(comboBox4, Convert.ToInt32(comboBox3.SelectedValue));
                }
            }
            else
            {
                comboBox4.DataSource = null;
            }
        }

        private void comboBox4_SelectedValueChanged(object sender, EventArgs e)
        {
            Type t = typeof(Int32);
            if (comboBox4.SelectedValue != null)
            {
                if (comboBox4.SelectedValue.GetType() == t)
                {
                    int id1;
                    id1 = Convert.ToInt32(comboBox4.SelectedValue);
                    count3 = Int32.Parse(ABase.SelectField("product", "count1", "id", id1));
                    price1 = Double.Parse(ABase.SelectField("product", "price", "id", id1));
                }
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (textBox1.Text.Length > 0)
            {
                if (flag1)
                {
                    var t1 = Double.Parse(textBox1.Text);
                    var res = t1*count3;
                    textBox2.Text = Convert.ToString(res);
                }
                else
                {
                    var t1 = Double.Parse(textBox1.Text);
                    textBox2.Text = Convert.ToString(t1);
                }
            }
            else
            {
                textBox1.Text = "0";
            }
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            if (textBox2.Text.Length > 0)
            {
                var t1 = Double.Parse(textBox2.Text);
                var res = t1 * price1;
                textBox3.Text = Convert.ToString(res);
                var temp = t1/count3;
            }
            else
            {
                textBox2.Text = "0";
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            var t1 = Double.Parse(textBox5.Text);
            if (textBox4.Text.Length>0)
            {
                var t2 = Double.Parse(textBox4.Text);
                if (t2>t1)
                {
                    MessageBox.Show("Значение остатка меньще выплаченного!!!");
                    return;
                }
                var res = t1 - t2;
                textBox5.Text = Convert.ToString(res);
                ABase.UpdateValueZakupka(Convert.ToString(t2),Convert.ToString(t1),id);
                ABase.InsertValueRas1(id, Convert.ToString(t2), Convert.ToString(t1),DateTime.Today.ToShortDateString());
                if (textBox3.Text != textBox5.Text || (textBox3.Text=="0"&&textBox5.Text=="0"))
                {
                    checkBox2.Enabled = checkBox1.Enabled = button4.Enabled = comboBox1.Enabled = comboBox2.Enabled = comboBox3.Enabled = comboBox4.Enabled = false;
                    textBox7.ReadOnly = textBox1.ReadOnly = true;
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string sklad = Convert.ToString(comboBox1.SelectedValue);
            string category= Convert.ToString(comboBox2.SelectedValue);
            string firma = Convert.ToString(comboBox3.SelectedValue);
            string product = Convert.ToString(comboBox4.SelectedValue);
            string count1 = textBox1.Text;
            string count2 = textBox2.Text;
            string summa = textBox3.Text;
            string raschet = textBox4.Text;
            string ostatok = textBox5.Text;
            ABase.UpdateValueZakupka(sklad,category,firma,product,count1,count2,summa,raschet,ostatok,id);
            Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            textBox5.Text = textBox3.Text;
        }

        private void textBox7_TextChanged(object sender, EventArgs e)
        {
            if (textBox7.Text.Length > 0)
            {
                var t = Double.Parse(textBox7.Text);
                if (flag)
                {
                    var res = t*count3*price1;
                    textBox6.Text = Convert.ToString(t*count3);
                    textBox8.Text = Convert.ToString(res);
                }
                else
                {
                    var res = t*price1;
                    textBox6.Text = Convert.ToString(t);
                    textBox8.Text = Convert.ToString(res);
                }
            }
            else
            {
                textBox7.Text = "0";
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            var t = Double.Parse(textBox2.Text);
            var t1 = Double.Parse(textBox6.Text);
            string summa = textBox8.Text;
            if (t1 < t)
            {
                var res = t - t1;
                textBox2.Text = Convert.ToString(res);
                ABase.InsertValueVozvrat1(id,Convert.ToString(t1),summa,DateTime.Today.ToShortDateString(),date);
            }
            else
            {
                MessageBox.Show("Значение возврата больше закупаемого количества");
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            flag = checkBox1.Checked;
            textBox7_TextChanged(null,null);
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            flag1 = checkBox2.Checked;
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8)
            {
                e.Handled = true;
            }
            else
            {
                e.Handled = false;
            }
        }

        private void textBox7_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8)
            {
                e.Handled = true;
            }
            else
            {
                e.Handled = false;
            }
        }
    }
}
